import { message } from '@umijs/plugin-request/lib/ui';
import { BaseUrl } from '@/config/env';
import axios, { AxiosRequestConfig } from 'axios';
import { history } from 'umi';
/*
 * @Author: your name
 * @Date: 2021-04-19 15:13:47
 * @LastEditTime: 2021-06-06 18:02:51
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /admin/src/utils/request.ts
 */
declare module 'axios' {
  interface AxiosRequestConfig {
    returnError?: boolean;
  }
}

export const request = axios.create({
  timeout: 5 * 60 * 1000,
  baseURL: BaseUrl,
  withCredentials: true,
});

request.interceptors.request.use((request: AxiosRequestConfig) => {
  return request;
});

request.interceptors.response.use((response) => {
  if (response.data.code === -2) {
    message.error(response.data.message);
    history.push('/login');
    return Promise.reject(response.data);
    // window.location.href('');
  } else if (response.data.code === -4 || response.data.code === -5) {
    return Promise.reject(response.data);
  }

  if (response.data.code !== 0) {
    message.error(response.data.message);
    return Promise.reject(response.data);
  }
  console.log(response.data.results, 'response==');

  return Promise.resolve(response);
});
